const router = require('express').Router();
const mongoose = require('mongoose');
const WorkerModel = mongoose.models['Worker'];
router.get('/', async function (req, res, next) {
    res.json({});
});

router.post('/addWorker', async function (req, res, next) {
    req.body.workType=req.body.workType.split(',');
    let worker = await WorkerModel.addWorker(req.body);
    if (!!worker) {
        return res.data(worker);
    } else {
        next('添加用户失败！');
    }
});

router.post('/modifyWorker', async function (req, res, next) {
    req.body.workType=req.body.workType.split(',');
    let worker = await WorkerModel.modifyWorker(req.body);
    if (!!worker) {
        return res.data(worker);
    } else {
        next('修改用户失败！');
    }
});

router.post('/showWorkers', async function (req, res, next) {
    let worker = await WorkerModel.showWorkers(req.body);
    if (!!worker) {
        return res.data(worker);
    } else {
        next('查询数据失败！');
    }
});


router.post('/deleteWorker', async function (req, res, next) {
    let worker = await WorkerModel.deleteWorker(req.body);
    if (!!worker) {
        return res.data(worker);
    } else {
        next('删除用户失败！');
    }
});

router.post('/findWorkers', async function (req, res, next) {
    if(req.body.workType.length > 0){
        req.body.workType=req.body.workType.split(',');
    }
    let worker = await WorkerModel.findWorkers(req.body);
    if (!!worker) {
        return res.data(worker);
    } else {
        next('查询数据失败！');
    }
});

router.post('/add',async function(req,res,next){
    try{
        if(req.body.workType.length > 0){
            req.body.workType=req.body.workType.split(',');
        }
        if(req.body.expectCities.length > 0){
            req.body.expectCities=req.body.expectCities.split(',');
        }
        await WorkerModel.add(req.body);
        res.data(true);
    }catch(e){
        next(e.message);
    }
});

router.post('/update',async function(req,res,next){
    try{
        let _id=req.body._id;
        if(!_id){
            throw new Error('你要更新的内容不存在！')
        }
        delete req.body._id;
        if(req.body.workType.length > 0){
            req.body.workType=req.body.workType.split(',');
        }
        if(req.body.expectCities.length > 0){
            req.body.expectCities=req.body.expectCities.split(',');
        }
        await WorkerModel.findByIdAndUpdate(_id,{$set:req.body});
        res.data(true);
    }catch(e){
        next(e.message);
    }
});

router.get('/list',async function(req,res,next){
    try{
        if(!!req.query.workType&&req.query.workType.length > 0){
            req.query.workType=req.query.workType.split(',');
        }
        let queryObj={};
        !!req.query.name&&(queryObj.name=req.query.name);
        !!req.query.phone&&(queryObj.phone=req.query.phone);
        !!req.query.idCard&&(queryObj.idCard=req.query.idCard);
        !!req.query.workType&&(queryObj.workType={$all:[req.query.workType]});
        !!req.query.expectCity&&(queryObj.expectCities={$all:[req.query.expectCity]});
        let data=await WorkerModel.list(parseInt(req.query.page),parseInt(req.query.pageCount),queryObj);
        res.data(data);
    }catch(e){
        next(e.message);
    }
});

router.get('/remove',async function(req,res,next){
    try{
        if(!req.query._id){
            throw new Error('你要删除的内容不存在！')
        }
        await WorkerModel.remove({_id:req.query._id});
        res.data(true);
    }catch(e){
        next(e.message);
    }
});

module.exports = router;